import com.baoxian.cfg.SysCfg
import com.baoxian.workschedule.TaskWorkSchedule
import com.baoxian.workschedule.mapper.TaskWorkScheduleXmlLoader
import com.baoxian.workschedule.util.CommonUtil
import com.baoxian.workschedule.util.ConstantsUtil

/**
 * User: ZhengCongChun
 * Date: 13-7-12
 * Time: 下午5:13
 * @Version 1.0
 *      To change this template use File | Settings | File Templates.
 */
class TaskWorkScheduleTest extends GroovyTestCase {
    String configFilePath = "D:\\work\\projects\\workschedule\\trunk\\baoxian-task-work-schedule\\src\\main\\resources\\baoxian-task-workschedule.xml"
//    public void setUp() {
//        SysCfg.init(Arrays.asList('D:\\work\\config\\root\\development\\project\\cm'), null)
//    }

    public void testWeekDay() {
        String date = "2013-09-01 12:00:00"
        println(CommonUtil.dayForWeek(date))
        println(CommonUtil.dayForWeek("2013-09-01 00:00:00"))
        println(CommonUtil.dayForWeek("2013-09-02 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-03 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-04 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-05 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-06 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-07 12:00:00"))
        println(CommonUtil.dayForWeek("2013-09-08 25:00:00"))
    }

    public void testIsCanProcess() {
        def result = TaskWorkSchedule.getInstance().isCanProcessTask("1212000000", ConstantsUtil.TASK_TYPE_PAYMENT)
        println(result)
    }

    public void testIsCanDo() {
        //SysCfg.init(Arrays.asList('D:\\work\\config\\root\\development\\project\\cm'), null)
        TaskWorkScheduleXmlLoader.init(configFilePath)
//        Map verifyResult = TaskWorkSchedule.getInstance().isCanProcessTask('1237000000', ConstantsUtil.TASK_TYPE_VERIFY, '2013-09-19 17:40:11')
//        Map secondPaymentResult = TaskWorkSchedule.getInstance().isCanProcessTask('1212000000', ConstantsUtil.TASK_TYPE_SECOND_PAYMENT, '2013-09-19 17:40:11')
        Map payment = TaskWorkSchedule.getInstance().isCanProcessTask('1244000000', ConstantsUtil.TASK_TYPE_PAYMENT, '2013-09-01 10:10:10')
        println(payment)
//        println(secondPaymentResult)
    }


   /**
    * 测试公共 节假日
    *  <holiday>
    <startDate>2013-10-01</startDate>
    <endDate>2013-10-07</endDate>
    <startTime>08:30</startTime>
    <endTime>18:00</endTime>
    </holiday>
    ************测试已通过************
    */
    public void testIsCapProcessTask0() {
        String orgId = "4008528528"
        String dateStr =   "2013-10-02 11:35:20"
        processFalseTask(orgId,dateStr)
    }

    /**
     * 测试属于特别指定节假日
     * <specialHoliday>
     <startDate>2013-07-22</startDate>
     <endDate>2013-07-26</endDate>
     <startTime>08:30</startTime>
     <endTime>18:00</endTime>
     </specialHoliday>
     ************测试已通过************
     */
    public void testIsCapProcessTask1() {
        String orgId = "4008528528"
        String dateStr ="2013-07-23 10:19:17"
         processFalseTask(orgId,dateStr)
    }

    /**
     * 测试属于特别指定工作日   正常时间范围 与非正常时间范围
     * <specialWorkDayTime>
     <startDate>2013-08-05</startDate>
     <endDate>2013-08-11</endDate>
     <startTime>08:30</startTime>
     <endTime>12:00</endTime>
     </specialWorkDayTime>
     ************测试已通过************
     */
    public void testIsCapProcessTask2() {
        String orgId = "4008528528"
//        String dateStr ="2013-08-06 10:19:17"    //正常时间
       String dateStr ="2013-08-06 19:19:17"    //非工作时间
       // String dateStr ="2013-08-06 10:19:17"    //正常时间
         processTrueTask(orgId,dateStr)
    }
    /**
     * 测试属于正常 时间工作日范围内的 （周一至五）
     ************测试已通过************
     */
    public void testIsCapProcessTask3() {
       // TaskWorkSchedule taskWorkSchedule= TaskWorkSchedule.getInstance()
        String orgId = "4008528528"
        String dateStr ="2013-09-04 11:19:17"    //正常时间 周五
        // String dateStr ="2013-09-06 13:19:17"    //非工作时间    周五
          processTrueTask(orgId,dateStr)
    }

    /**
     *    周未特别指定时间
     ************测试已通过************
     */
    public void testIsCapProcessTask4() {
        String orgId = "4008528528"
        String dateStr ="2013-09-06 10:19:17"    //正常时间 周五
        // String dateStr ="2013-09-06 13:19:17"    //非工作时间    周五
         processTrueTask(orgId,dateStr)
    }


    /**
     *  测试 机构  配置
      *************当重复任务时       程序以后面配置的为准 ********
     * ***********当节点未配置时      程序使用的全局节点*************
     * ***********当同时有两个机构节点时   会程序以后添加的节点为准***************
     */
    public void testIsCapProcessTask5() {
        String orgId = "4008528528"
        String dateStr ="2013-09-03 12:19:17"    //正常时间 周二
        // String dateStr ="2013-09-03 18:19:17"    //非工作时间    周二
        processTrueTask(orgId,dateStr)
    }
    /**
     * @param 判断是可以处理的任务
     * @param dateStr
     */
     public void processTrueTask(String orgId ,String dateStr){
         TaskWorkSchedule taskWorkSchedule= TaskWorkSchedule.getInstance()
         Map<String, Object> result = null
         result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_VERIFY,dateStr)      //初审
         assertNotNull(result)
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info( ConstantsUtil.TASK_TYPE_VERIFY + "    " + result)

         result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_QUOTE,dateStr)  //报价
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_QUOTE + "    " + result)

         result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INSURE,dateStr)  //核保
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info( ConstantsUtil.TASK_TYPE_INSURE + "    " + result)

         result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_PAYMENT,dateStr)  //支付
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_PAYMENT + "    " + result)

         result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_SECOND_PAYMENT,dateStr)  //二次支付
         assertFalse(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info( ConstantsUtil.TASK_TYPE_SECOND_PAYMENT + "    " + result)

         result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_UNDERWRITE,dateStr)  //承保
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_UNDERWRITE + " " + result)

         result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INS_POLICY_SYNC,dateStr)  //录单
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_INS_POLICY_SYNC + "    " + result)

         result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INS_POLICY_PRINTING,dateStr)  //打单
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_INS_POLICY_PRINTING + "    " + result)

         result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_DELIVERY,dateStr)  //配送
         assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
         log.info(ConstantsUtil.TASK_TYPE_DELIVERY + "    " + result)
     }

    /**
     * 判断是不可以处理的任务
     * @param orgId
     * @param dateStr
     */
    public void processFalseTask(String orgId ,String dateStr){
        TaskWorkSchedule taskWorkSchedule= TaskWorkSchedule.getInstance()
        Map<String, Object> result = null
        result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_VERIFY,dateStr)      //初审
      //  assertNotNull(result)
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info( ConstantsUtil.TASK_TYPE_VERIFY + "    " + result)

        result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_QUOTE,dateStr)  //报价
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_QUOTE + "    " + result)

        result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INSURE,dateStr)  //核保
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info( ConstantsUtil.TASK_TYPE_INSURE + "    " + result)

        result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_PAYMENT,dateStr)  //支付
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_PAYMENT + "    " + result)

        result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_SECOND_PAYMENT,dateStr)  //二次支付
        assertFalse(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info( ConstantsUtil.TASK_TYPE_SECOND_PAYMENT + "    " + result)

        result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_UNDERWRITE,dateStr)  //承保
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_UNDERWRITE + "    " + result)

        result= taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INS_POLICY_SYNC,dateStr)  //录单
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_INS_POLICY_SYNC + "    " + result)

        result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_INS_POLICY_PRINTING,dateStr)  //打单
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_INS_POLICY_PRINTING + "    " + result)

        result=  taskWorkSchedule.isCanProcessTask(orgId, ConstantsUtil.TASK_TYPE_DELIVERY,dateStr)  //配送
        assertTrue(result.get(ConstantsUtil.IS_CAN_PROCESS))
        log.info(ConstantsUtil.TASK_TYPE_DELIVERY + "    " + result)
    }

    public void testIsCapProcessTask() {
        println(TaskWorkSchedule.getInstance().isCanProcessTask("4008528528", ConstantsUtil.TASK_TYPE_VERIFY))
    }

    /**
     * 判断机构ID否在配置文件中有配置
     */
    public void testExitsOrg() {
        println(TaskWorkSchedule.getInstance().isExitsOraganization('400s8528528'))
    }
}
